"""
妙码类型信息定义模块
"""
from typing import Optional, Any
from dataclasses import dataclass
import yaml
from pathlib import Path
from .types import MiaomaType

@dataclass
class MiaomaTypeInfo:
    """妙码类型信息类"""
    type: MiaomaType              # 类型
    base_type: Optional[MiaomaType] = None  # 基础类型，基础类型为None，扩展类型指向其基础类型
    ts_type: Optional[str] = None           # TypeScript类型
    vue_component: Optional[str] = None      # Vue组件
    vue_antd_component: Optional[str] = None # Ant Design Vue组件
    java_type: Optional[str] = None         # Java类型
    max_length: Optional[int] = None        # 最大长度
    min_length: Optional[int] = None        # 最小长度
    max_value: Optional[Any] = None         # 最大值
    min_value: Optional[Any] = None         # 最小值
    default_value: Any = None               # 默认值
    placeholder: Optional[str] = None        # 占位符
    mock_rule: Optional[str] = None         # Mock规则
    datetime_storage_format: Optional[str] = None  # 日期时间存储格式
    validation_pattern: Optional[str] = None      # 验证规则

def load_type_info() -> dict:
    """加载类型信息配置"""
    config_path = Path(__file__).parent / 'type_info.yaml'
    with open(config_path, 'r', encoding='utf-8') as f:
        config = yaml.safe_load(f)

    type_info_map = {}

    # 加载基础类型配置
    for type_name, type_config in config.get('base_types', {}).items():
        type_enum = getattr(MiaomaType, type_name)
        type_info_map[type_enum] = MiaomaTypeInfo(
            type=type_enum,
            **type_config
        )

    # 加载扩展类型配置
    for type_name, type_config in config.get('extended_types', {}).items():
        type_enum = getattr(MiaomaType, type_name)
        # 获取并移除base_type配置
        base_type_name = type_config.pop('base_type')
        base_type_enum = getattr(MiaomaType, base_type_name)

        # 获取基础类型的配置
        base_config = type_info_map[base_type_enum].__dict__.copy()
        # 移除不需要继承的字段
        base_config.pop('type')
        base_config.pop('base_type')

        # 合并配置，扩展类型的配置优先
        merged_config = {**base_config, **type_config}

        type_info_map[type_enum] = MiaomaTypeInfo(
            type=type_enum,
            base_type=base_type_enum,
            **merged_config
        )

    return type_info_map

# 加载类型信息
MIAOMA_TYPE_INFO_MAP = load_type_info()